using System;
using System.Collections;
using System.Collections.Generic;
using Server;
using Server.Items;
using Server.Mobiles;
using Server.Gumps;
using Server.Targeting;
using Server.Multis;
using Server.ContextMenus;
using Server.Regions;
using Server.Misc;


namespace Server.Mobiles
{
	public class RestockableVendor : WanderingTrader
	{
		private ArrayList m_SBInfos = new ArrayList();
		protected override ArrayList SBInfos{ get { return m_SBInfos; } }

		public static readonly int MaxStackable = 60000;	// Maximum amount of stackable items for Restockable vendor
		public static readonly int MaxQuantity = 999;			// Max amount of non-stackable items for Restockable vendor

		private ArrayList m_ExternalBuyInfo;
		private bool m_IsBulk;

		public ArrayList ExternalBuyInfo
		{
			get{ return m_ExternalBuyInfo; }
			set{ m_ExternalBuyInfo = value; }
		}

		[CommandProperty( AccessLevel.GameMaster)]
		public bool IsBulk
		{
			get{ return m_IsBulk; }
			set
			{
				m_IsBulk = value;

				if ( m_IsBulk )
					this.Title = "the Bulk Orderer";
				else
					this.Title = "the Restocker";

				InvalidateProperties();
			}
		}

		public ArrayList BInfo { get{ return m_SBInfos; } }

		[Constructable]
		public RestockableVendor( Mobile owner, VendorManager vm ) : this( owner, vm, "the Restocker" )
		{
		}

		[Constructable]
		public RestockableVendor( Mobile owner, VendorManager vm, string title ) : base( owner, vm, title )
		{
			ShopItems = new Hashtable();
			Currency = typeof( Gold );

			AllowBid = false;
			BondOnly = false;
			m_IsBulk = false;

			Owner = owner;
			Manager = vm;

			m_ExternalBuyInfo = new ArrayList();
		}

		public RestockableVendor( Serial serial ) : base( serial )
		{
		}

		public override void InitSBInfo()
		{
			m_SBInfos.Add( new SBRestockableVendor( this ) );
		}

		public override void Restock()
		{
			base.LoadSBInfo();

			LastRestock = DateTime.Now;

			IBuyItemInfo[] buyInfo = this.GetBuyInfo();

			foreach ( IBuyItemInfo bii in buyInfo )
			{
				if ( m_IsBulk )
					bii.Amount = bii.MaxAmount;
				else
					bii.OnRestock();
			}
		}


		public override void Serialize( GenericWriter writer )
		{
			base.Serialize( writer );
			writer.Write( (int) 1 ); // version

			writer.Write( (bool) m_IsBulk );

			if ( m_ExternalBuyInfo != null && m_ExternalBuyInfo.Count > 0 )
			{
				writer.Write( (int) m_ExternalBuyInfo.Count );

				foreach ( GenericBuyInfo bi in m_ExternalBuyInfo )
				{
					writer.Write( (string) bi.Type.Name );
					writer.Write( (string) bi.Name );
					writer.Write( (int) bi.Price );
					writer.Write( (int) bi.MaxAmount );
					writer.Write( (int) bi.ItemID );
					writer.Write( (int) bi.Hue );
				}
			}
			else
				writer.Write( (int) 0 );
		}

		public override void Deserialize( GenericReader reader )
		{
			base.Deserialize( reader );
			int version = reader.ReadInt();

			if ( version > 0 )
				m_IsBulk = reader.ReadBool();

			m_ExternalBuyInfo = new ArrayList();

			for ( int i = reader.ReadInt(); i > 0; i-- )
			{
				Type type = ScriptCompiler.FindTypeByName( reader.ReadString() );
				GenericBuyInfo bi = new GenericBuyInfo( reader.ReadString(), type, reader.ReadInt(), reader.ReadInt(), reader.ReadInt(), reader.ReadInt() );

				if ( bi != null )
					m_ExternalBuyInfo.Add( bi );
			}

			Restock();
		}

	}
}